home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2008 February / PCWFEB08.iso / Software / Resources / Developers / XAMPP 1.5.4 / Windows installer / xampp-win32-1.5.4-installer.exe / xampp / php / pear / Config / Container.php
Encoding:
PHP Script  |  2006-04-07  |  26.1 KB  |  771 lines

  1. <?php
  2. // +---------------------------------------------------------------------+
  3. // | PHP Version 4                                                       |
  4. // +---------------------------------------------------------------------+
  5. // | Copyright (c) 1997, 1998, 1999, 2000, 2001 The PHP Group            |
  6. // +---------------------------------------------------------------------+
  7. // | This source file is subject to version 2.0 of the PHP license,      |
  8. // | that is bundled with this package in the file LICENSE, and is       |
  9. // | available at through the world-wide-web at                          |
  10. // | http://www.php.net/license/2_02.txt.                                |
  11. // | If you did not receive a copy of the PHP license and are unable to  |
  12. // | obtain it through the world-wide-web, please send a note to         |
  13. // | license@php.net so we can mail you a copy immediately.              |
  14. // +---------------------------------------------------------------------+
  15. // | Author: Bertrand Mansion <bmansion@mamasam.com>                     |
  16. // +---------------------------------------------------------------------+
  17. //
  18. // $Id: Container.php,v 1.40 2006/02/14 00:47:46 aashley Exp $
  19.  
  20. require_once 'Config.php';
  21.  
  22. /**
  23. * Interface for Config containers
  24. *
  25. * @author   Bertrand Mansion <bmansion@mamasam.com>
  26. * @package  Config
  27. */
  28. class Config_Container {
  29.  
  30.     /**
  31.     * Container object type
  32.     * Ex: section, directive, comment, blank
  33.     * @var  string
  34.     */
  35.     var $type;
  36.  
  37.     /**
  38.     * Container object name
  39.     * @var  string
  40.     */
  41.     var $name = '';
  42.  
  43.     /**
  44.     * Container object content
  45.     * @var  string
  46.     */
  47.     var $content = '';
  48.  
  49.     /**
  50.     * Container object children
  51.     * @var  array
  52.     */
  53.     var $children = array();
  54.  
  55.     /**
  56.     * Reference to container object's parent
  57.     * @var  object
  58.     */
  59.     var $parent;
  60.     
  61.     /**
  62.     * Array of attributes for this item
  63.     * @var  array
  64.     */
  65.     var $attributes;
  66.  
  67.     /**
  68.     * Unique id to differenciate nodes
  69.     *
  70.     * This is used to compare nodes
  71.     * Will not be needed anymore when this class will use ZendEngine 2
  72.     *
  73.     * @var  int
  74.     */
  75.     var $_id;
  76.  
  77.     /**
  78.     * Constructor
  79.     *
  80.     * @param  string  $type       Type of container object
  81.     * @param  string  $name       Name of container object
  82.     * @param  string  $content    Content of container object
  83.     * @param  array   $attributes Array of attributes for container object
  84.     */
  85.     function Config_Container($type = 'section', $name = '', $content = '', $attributes = null)
  86.     {
  87.         $this->type       = $type;
  88.         $this->name       = $name;
  89.         $this->content    = $content;
  90.         $this->attributes = $attributes;
  91.         $this->parent     = null;
  92.         if (version_compare(PHP_VERSION, '5.0.0', 'gt')) {
  93.             $this->_id    = uniqid($name.$type, true);
  94.         } else {
  95.             $this->_id    = uniqid(substr($name.$type, 0, 114), true);
  96.         }
  97.     } // end constructor
  98.  
  99.     /**
  100.     * Create a child for this item.
  101.     * @param  string  $type       type of item: directive, section, comment, blank...
  102.     * @param  mixed   $item       item name
  103.     * @param  string  $content    item content
  104.     * @param  array   $attributes item attributes
  105.     * @param  string  $where      choose a position 'bottom', 'top', 'after', 'before'
  106.     * @param  object  $target     needed if you choose 'before' or 'after' for where
  107.     * @return object  reference to new item or Pear_Error
  108.     */
  109.     function &createItem($type, $name, $content, $attributes = null, $where = 'bottom', $target = null)
  110.     {
  111.         $item =& new Config_Container($type, $name, $content, $attributes);
  112.         $result =& $this->addItem($item, $where, $target);
  113.         return $result;
  114.     } // end func &createItem
  115.     
  116.     /**
  117.     * Adds an item to this item.
  118.     * @param  object   $item      a container object
  119.     * @param  string   $where     choose a position 'bottom', 'top', 'after', 'before'
  120.     * @param  object   $target    needed if you choose 'before' or 'after' in $where
  121.     * @return mixed    reference to added container on success, Pear_Error on error
  122.     */
  123.     function &addItem(&$item, $where = 'bottom', $target = null)
  124.     {
  125.         if ($this->type != 'section') {
  126.             return PEAR::raiseError('Config_Container::addItem must be called on a section type object.', null, PEAR_ERROR_RETURN);
  127.         }
  128.         if (is_null($target)) {
  129.             $target =& $this;
  130.         }
  131.         if (strtolower(get_class($target)) != 'config_container') {
  132.             return PEAR::raiseError('Target must be a Config_Container object in Config_Container::addItem.', null, PEAR_ERROR_RETURN);
  133.         }
  134.  
  135.         switch ($where) {
  136.             case 'before':
  137.                 $index = $target->getItemIndex();
  138.                 break;
  139.             case 'after':
  140.                 $index = $target->getItemIndex()+1;
  141.                 break;
  142.             case 'top':
  143.                 $index = 0;
  144.                 break;
  145.             case 'bottom':
  146.                 $index = -1;
  147.                 break;
  148.             default:
  149.                 return PEAR::raiseError('Use only top, bottom, before or after in Config_Container::addItem.', null, PEAR_ERROR_RETURN);
  150.         }
  151.         if (isset($index) && $index >= 0) {
  152.             array_splice($this->children, $index, 0, 'tmp');
  153.         } else {
  154.             $index = count($this->children);
  155.         }
  156.         $this->children[$index] =& $item;
  157.         $this->children[$index]->parent =& $this;
  158.  
  159.         return $item;
  160.     } // end func addItem
  161.  
  162.     /**
  163.     * Adds a comment to this item.
  164.     * This is a helper method that calls createItem
  165.     *
  166.     * @param  string    $content        Object content
  167.     * @param  string    $where          Position : 'top', 'bottom', 'before', 'after'
  168.     * @param  object    $target         Needed when $where is 'before' or 'after'
  169.     * @return object  reference to new item or Pear_Error
  170.     */
  171.     function &createComment($content = '', $where = 'bottom', $target = null)
  172.     {
  173.         return $this->createItem('comment', null, $content, null, $where, $target);
  174.     } // end func &createComment
  175.  
  176.     /**
  177.     * Adds a blank line to this item.
  178.     * This is a helper method that calls createItem
  179.     *
  180.     * @return object  reference to new item or Pear_Error
  181.     */
  182.     function &createBlank($where = 'bottom', $target = null)
  183.     {
  184.         return $this->createItem('blank', null, null, null, $where, $target);
  185.     } // end func &createBlank
  186.  
  187.     /**
  188.     * Adds a directive to this item.
  189.     * This is a helper method that calls createItem
  190.     *
  191.     * @param  string    $name           Name of new directive
  192.     * @param  string    $content        Content of new directive
  193.     * @param  mixed     $attributes     Directive attributes
  194.     * @param  string    $where          Position : 'top', 'bottom', 'before', 'after'
  195.     * @param  object    $target         Needed when $where is 'before' or 'after'
  196.     * @return object  reference to new item or Pear_Error
  197.     */
  198.     function &createDirective($name, $content, $attributes = null, $where = 'bottom', $target = null)
  199.     {
  200.         return $this->createItem('directive', $name, $content, $attributes, $where, $target);
  201.     } // end func &createDirective
  202.  
  203.     /**
  204.     * Adds a section to this item.
  205.     *
  206.     * This is a helper method that calls createItem
  207.     * If the section already exists, it won't create a new one. 
  208.     * It will return reference to existing item.
  209.     *
  210.     * @param  string    $name           Name of new section
  211.     * @param  array     $attributes     Section attributes
  212.     * @param  string    $where          Position : 'top', 'bottom', 'before', 'after'
  213.     * @param  object    $target         Needed when $where is 'before' or 'after'
  214.     * @return object  reference to new item or Pear_Error
  215.     */
  216.     function &createSection($name, $attributes = null, $where = 'bottom', $target = null)
  217.     {
  218.         return $this->createItem('section', $name, null, $attributes, $where, $target);
  219.     } // end func &createSection
  220.  
  221.     /**
  222.     * Tries to find the specified item(s) and returns the objects.
  223.     *
  224.     * Examples:
  225.     * $directives =& $obj->getItem('directive');
  226.     * $directive_bar_4 =& $obj->getItem('directive', 'bar', null, 4);
  227.     * $section_foo =& $obj->getItem('section', 'foo');
  228.     *
  229.     * This method can only be called on an object of type 'section'.
  230.     * Note that root is a section.
  231.     * This method is not recursive and tries to keep the current structure.
  232.     * For a deeper search, use searchPath()
  233.     *
  234.     * @param  string    $type        Type of item: directive, section, comment, blank...
  235.     * @param  mixed     $name        Item name
  236.     * @param  mixed     $content     Find item with this content
  237.     * @param  array     $attributes  Find item with attribute set to the given value
  238.     * @param  int       $index       Index of the item in the returned object list. If it is not set, will try to return the last item with this name.
  239.     * @return mixed  reference to item found or false when not found
  240.     * @see &searchPath()
  241.     */
  242.     function &getItem($type = null, $name = null, $content = null, $attributes = null, $index = -1)
  243.     {
  244.         if ($this->type != 'section') {
  245.             return PEAR::raiseError('Config_Container::getItem must be called on a section type object.', null, PEAR_ERROR_RETURN);
  246.         }
  247.         if (!is_null($type)) {
  248.             $testFields[] = 'type';
  249.         }
  250.         if (!is_null($name)) {
  251.             $testFields[] = 'name';
  252.         }
  253.         if (!is_null($content)) {
  254.             $testFields[] = 'content';
  255.         }
  256.         if (!is_null($attributes) && is_array($attributes)) {
  257.             $testFields[] = 'attributes';
  258.         }
  259.  
  260.         $itemsArr = array();
  261.         $fieldsToMatch = count($testFields);
  262.         for ($i = 0, $count = count($this->children); $i < $count; $i++) {
  263.             $match = 0;
  264.             reset($testFields);
  265.             foreach ($testFields as $field) {
  266.                 if ($field != 'attributes') {
  267.                     if ($this->children[$i]->$field == ${$field}) {
  268.                         $match++;
  269.                     }
  270.                 } else {
  271.                     // Look for attributes in array
  272.                     $attrToMatch = count($attributes);
  273.                     $attrMatch = 0;
  274.                     foreach ($attributes as $key => $value) {
  275.                         if (isset($this->children[$i]->attributes[$key]) &&
  276.                             $this->children[$i]->attributes[$key] == $value) {
  277.                             $attrMatch++;
  278.                         }
  279.                     }
  280.                     if ($attrMatch == $attrToMatch) {
  281.                         $match++;
  282.                     }
  283.                 }
  284.             }
  285.             if ($match == $fieldsToMatch) {
  286.                 $itemsArr[] =& $this->children[$i];
  287.             }
  288.         }
  289.         if ($index >= 0) {
  290.             if (isset($itemsArr[$index])) {
  291.                 return $itemsArr[$index];
  292.             } else {
  293.                 $return = false;
  294.                 return $return;
  295.             }
  296.         } else {
  297.             if ($count = count($itemsArr)) {
  298.                 return $itemsArr[$count-1];
  299.             } else {
  300.                 $return = false;
  301.                 return $return;
  302.             }
  303.         }
  304.     } // end func &getItem
  305.  
  306.     /**
  307.     * Finds a node using XPATH like format.
  308.     * 
  309.     * The search format is an array:
  310.     * array(item1, item2, item3, ...)
  311.     *
  312.     * Each item can be defined as the following:
  313.     * item = 'string' : will match the container named 'string'
  314.     * item = array('string', array('name' => 'xyz'))
  315.     * will match the container name 'string' whose attribute name is equal to "xyz"
  316.     * For example : <string name="xyz">
  317.     * 
  318.     * @param    mixed   Search path and attributes
  319.     * 
  320.     * @return   mixed   Config_Container object, array of Config_Container objects or false on failure.
  321.     * @access   public
  322.     */
  323.     function &searchPath($args)
  324.     {
  325.         if ($this->type != 'section') {
  326.             return PEAR::raiseError('Config_Container::searchPath must be called on a section type object.', null, PEAR_ERROR_RETURN);
  327.         }
  328.  
  329.         $arg = array_shift($args);
  330.  
  331.         if (is_array($arg)) {
  332.             $name = $arg[0];
  333.             $attributes = $arg[1];
  334.         } else {
  335.             $name = $arg;
  336.             $attributes = null;
  337.         }
  338.         // find all the matches for first..
  339.         $match =& $this->getItem(null, $name, null, $attributes);
  340.  
  341.         if (!$match) {
  342.             $return = false;
  343.             return $return;
  344.         }
  345.         if (!empty($args)) {
  346.             return $match->searchPath($args);
  347.         }
  348.         return $match;
  349.     } // end func &searchPath
  350.  
  351.     /**
  352.     * Return a child directive's content.
  353.     * 
  354.     * This method can use two different search approach, depending on
  355.     * the parameter it is given. If the parameter is an array, it will use
  356.     * the {@link Config_Container::searchPath()} method. If it is a string, 
  357.     * it will use the {@link Config_Container::getItem()} method.
  358.     *
  359.     * Example:
  360.     * <code>
  361.     * require_once 'Config.php';
  362.     * $ini = new Config();
  363.     * $conf =& $ini->parseConfig('/path/to/config.ini', 'inicommented');
  364.     *
  365.     * // Will return the value found at :
  366.     * // [Database]
  367.     * // host=localhost
  368.     * echo $conf->directiveContent(array('Database', 'host')));
  369.     *
  370.     * // Will return the value found at :
  371.     * // date="dec-2004"
  372.     * echo $conf->directiveContent('date');
  373.     *
  374.     * </code>
  375.     *
  376.     * @param    mixed   Search path and attributes or a directive name
  377.     * @param    int     Index of the item in the returned directive list.
  378.     *                   Eventually used if args is a string.
  379.     * 
  380.     * @return   mixed   Content of directive or false if not found.
  381.     * @access   public
  382.     */
  383.     function directiveContent($args, $index = -1)
  384.     {
  385.         if (is_array($args)) {
  386.             $item =& $this->searchPath($args);
  387.         } else {
  388.             $item =& $this->getItem('directive', $args, null, null, $index);
  389.         }
  390.         if ($item) {
  391.             return $item->getContent();
  392.         }
  393.         return false;
  394.     } // end func getDirectiveContent
  395.  
  396.     /**
  397.     * Returns how many children this container has
  398.     *
  399.     * @param  string    $type    type of children counted
  400.     * @param  string    $name    name of children counted
  401.     * @return int  number of children found
  402.     */
  403.     function countChildren($type = null, $name = null)
  404.     {
  405.         if (is_null($type) && is_null($name)) {
  406.             return count($this->children);
  407.         }
  408.         $count = 0;
  409.         if (isset($name) && isset($type)) {
  410.             for ($i = 0, $children = count($this->children); $i < $children; $i++) {
  411.                 if ($this->children[$i]->name === $name && 
  412.                     $this->children[$i]->type == $type) {
  413.                     $count++;
  414.                 }
  415.             }
  416.             return $count;
  417.         }
  418.         if (isset($type)) {
  419.             for ($i = 0, $children = count($this->children); $i < $children; $i++) {
  420.                 if ($this->children[$i]->type == $type) {
  421.                     $count++;
  422.                 }
  423.             }
  424.             return $count;
  425.         }
  426.         if (isset($name)) {
  427.             // Some directives can have the same name
  428.             for ($i = 0, $children = count($this->children); $i < $children; $i++) {
  429.                 if ($this->children[$i]->name === $name) {
  430.                     $count++;
  431.                 }
  432.             }
  433.             return $count;
  434.         }
  435.     } // end func &countChildren
  436.  
  437.     /**
  438.     * Deletes an item (section, directive, comment...) from the current object
  439.     * TODO: recursive remove in sub-sections
  440.     * @return mixed  true if object was removed, false if not, or PEAR_Error if root
  441.     */
  442.     function removeItem()
  443.     {
  444.         if ($this->isRoot()) {
  445.             return PEAR::raiseError('Cannot remove root item in Config_Container::removeItem.', null, PEAR_ERROR_RETURN);
  446.         }
  447.         $index = $this->getItemIndex();
  448.         if (!is_null($index)) {
  449.             array_splice($this->parent->children, $index, 1);
  450.             return true;
  451.         }
  452.         return false;
  453.     } // end func removeItem
  454.  
  455.     /**
  456.     * Returns the item index in its parent children array.
  457.     * @return int  returns int or null if root object
  458.     */
  459.     function getItemIndex()
  460.     {
  461.         if (is_object($this->parent)) {
  462.             // This will be optimized with Zend Engine 2
  463.             $pchildren =& $this->parent->children;
  464.             for ($i = 0, $count = count($pchildren); $i < $count; $i++) {
  465.                 if ($pchildren[$i]->_id == $this->_id) {
  466.                     return $i;
  467.                 }
  468.             }
  469.         }
  470.         return;
  471.     } // end func getItemIndex
  472.  
  473.     /**
  474.     * Returns the item rank in its parent children array
  475.     * according to other items with same type and name.
  476.     * @return int  returns int or null if root object
  477.     */
  478.     function getItemPosition()
  479.     {
  480.         if (is_object($this->parent)) {
  481.             $pchildren =& $this->parent->children;
  482.             for ($i = 0, $count = count($pchildren); $i < $count; $i++) {
  483.                 if ($pchildren[$i]->name == $this->name &&
  484.                     $pchildren[$i]->type == $this->type) {
  485.                     $obj[] =& $pchildren[$i];
  486.                 }
  487.             }
  488.             for ($i = 0, $count = count($obj); $i < $count; $i++) {
  489.                 if ($obj[$i]->_id == $this->_id) {
  490.                     return $i;
  491.                 }
  492.             }
  493.         }
  494.         return;
  495.     } // end func getItemPosition
  496.  
  497.     /**
  498.     * Returns the item parent object.
  499.     * @return object  returns reference to parent object or null if root object
  500.     */
  501.     function &getParent()
  502.     {
  503.         return $this->parent;
  504.     } // end func &getParent
  505.  
  506.     /**
  507.     * Returns the item parent object.
  508.     * @return mixed  returns reference to child object or false if child does not exist
  509.     */
  510.     function &getChild($index = 0)
  511.     {
  512.         if (!empty($this->children[$index])) {
  513.             return $this->children[$index];
  514.         } else {
  515.             return false;
  516.         }
  517.     } // end func &getChild
  518.  
  519.     /**
  520.     * Set this item's name.
  521.     * @return void
  522.     */
  523.     function setName($name)
  524.     {
  525.         $this->name = $name;
  526.     } // end func setName
  527.  
  528.     /**
  529.     * Get this item's name.
  530.     * @return string    item's name
  531.     */
  532.     function getName()
  533.     {
  534.         return $this->name;
  535.     } // end func getName
  536.  
  537.     /**
  538.     * Set this item's content.
  539.     * @return void
  540.     */
  541.     function setContent($content)
  542.     {
  543.         $this->content = $content;
  544.     } // end func setContent
  545.     
  546.     /**
  547.     * Get this item's content.
  548.     * @return string    item's content
  549.     */
  550.     function getContent()
  551.     {
  552.         return $this->content;
  553.     } // end func getContent
  554.  
  555.     /**
  556.     * Set this item's type.
  557.     * @return void
  558.     */
  559.     function setType($type)
  560.     {
  561.         $this->type = $type;
  562.     } // end func setType
  563.  
  564.     /**
  565.     * Get this item's type.
  566.     * @return string    item's type
  567.     */
  568.     function getType()
  569.     {
  570.         return $this->type;
  571.     } // end func getType
  572.  
  573.     /**
  574.     * Set this item's attributes.
  575.     * @param  array    $attributes        Array of attributes
  576.     * @return void
  577.     */
  578.     function setAttributes($attributes)
  579.     {
  580.         $this->attributes = $attributes;
  581.     } // end func setAttributes
  582.  
  583.     /**
  584.     * Set this item's attributes.
  585.     * @param  array    $attributes        Array of attributes
  586.     * @return void
  587.     */
  588.     function updateAttributes($attributes)
  589.     {
  590.         if (is_array($attributes)) {
  591.             foreach ($attributes as $key => $value) {
  592.                 $this->attributes[$key] = $value;
  593.             }
  594.         }
  595.     } // end func updateAttributes
  596.  
  597.     /**
  598.     * Get this item's attributes.
  599.     * @return array    item's attributes
  600.     */
  601.     function getAttributes()
  602.     {
  603.         return $this->attributes;
  604.     } // end func getAttributes
  605.     
  606.     /**
  607.     * Get one attribute value of this item
  608.     * @param  string   $attribute        Attribute key
  609.     * @return mixed    item's attribute value
  610.     */
  611.     function getAttribute($attribute)
  612.     {
  613.         if (isset($this->attributes[$attribute])) {
  614.             return $this->attributes[$attribute];
  615.         }
  616.         return null;
  617.     } // end func getAttribute
  618.  
  619.     /**
  620.     * Set a children directive content.
  621.     * This is an helper method calling getItem and addItem or setContent for you.
  622.     * If the directive does not exist, it will be created at the bottom.
  623.     *
  624.     * @param  string    $name        Name of the directive to look for
  625.     * @param  mixed     $content     New content
  626.     * @param  int       $index       Index of the directive to set,
  627.     *                                in case there are more than one directive
  628.     *                                with the same name
  629.     * @return object    newly set directive
  630.     */
  631.     function &setDirective($name, $content, $index = -1)
  632.     {
  633.         $item =& $this->getItem('directive', $name, null, null, $index);
  634.         if ($item === false || PEAR::isError($item)) {
  635.             // Directive does not exist, will create one
  636.             unset($item);
  637.             return $this->createDirective($name, $content, null);
  638.         } else {
  639.             // Change existing directive value
  640.             $item->setContent($content);
  641.             return $item;
  642.         }
  643.     } // end func setDirective
  644.  
  645.     /**
  646.     * Is this item root, in a config container object
  647.     * @return bool    true if item is root
  648.     */
  649.     function isRoot()
  650.     {
  651.         if (is_null($this->parent)) {
  652.             return true;
  653.         }
  654.         return false;
  655.     } // end func isRoot
  656.  
  657.     /**
  658.     * Call the toString methods in the container plugin
  659.     * @param    string  $configType  Type of configuration used to generate the string
  660.     * @param    array   $options     Specify special options used by the parser
  661.     * @return   mixed   true on success or PEAR_ERROR
  662.     */
  663.     function toString($configType, $options = array())
  664.     {
  665.         $configType = strtolower($configType);
  666.         if (!isset($GLOBALS['CONFIG_TYPES'][$configType])) {
  667.             return PEAR::raiseError("Configuration type '$configType' is not registered in Config_Container::toString.", null, PEAR_ERROR_RETURN);
  668.         }
  669.         $includeFile = $GLOBALS['CONFIG_TYPES'][$configType][0];
  670.         $className   = $GLOBALS['CONFIG_TYPES'][$configType][1];
  671.         include_once($includeFile);
  672.         $renderer = new $className($options);
  673.         return $renderer->toString($this);
  674.     } // end func toString
  675.  
  676.     /**
  677.     * Returns a key/value pair array of the container and its children.
  678.     *
  679.     * Format : section[directive][index] = value
  680.     * If the container has attributes, it will use '@' and '#'
  681.     * index is here because multiple directives can have the same name.
  682.     *
  683.     * @param    bool    $useAttr        Whether to return the attributes too
  684.     * @return array
  685.     */
  686.     function toArray($useAttr = true)
  687.     {
  688.         $array[$this->name] = array();
  689.         switch ($this->type) {
  690.             case 'directive':
  691.                 if ($useAttr && count($this->attributes) > 0) {
  692.                     $array[$this->name]['#'] = $this->content;
  693.                     $array[$this->name]['@'] = $this->attributes;
  694.                 } else {
  695.                     $array[$this->name] = $this->content;
  696.                 }
  697.                 break;
  698.             case 'section':
  699.                 if ($useAttr && count($this->attributes) > 0) {
  700.                     $array[$this->name]['@'] = $this->attributes;
  701.                 }
  702.                 if ($count = count($this->children)) {
  703.                     for ($i = 0; $i < $count; $i++) {
  704.                         $newArr = $this->children[$i]->toArray($useAttr);
  705.                         if (!is_null($newArr)) {
  706.                             foreach ($newArr as $key => $value) {
  707.                                 if (isset($array[$this->name][$key])) {
  708.                                     // duplicate name/type
  709.                                     if (!is_array($array[$this->name][$key]) ||
  710.                                         !isset($array[$this->name][$key][0])) {
  711.                                         $old = $array[$this->name][$key];
  712.                                         unset($array[$this->name][$key]);
  713.                                         $array[$this->name][$key][0] = $old;
  714.                                     }
  715.                                     $array[$this->name][$key][] = $value;
  716.                                 } else {
  717.                                     $array[$this->name][$key] = $value;
  718.                                 }
  719.                             }
  720.                         }
  721.                     }
  722.                 }
  723.                 break;
  724.             default:
  725.                 return null;
  726.         }
  727.         return $array;
  728.     } // end func toArray
  729.     
  730.     /**
  731.     * Writes the configuration to a file
  732.     * 
  733.     * @param  mixed  $datasrc        Info on datasource such as path to the configuraton file or dsn...
  734.     * @param  string $configType     Type of configuration
  735.     * @param  array  $options        Options for writer
  736.     * @access public
  737.     * @return mixed     true on success or PEAR_ERROR
  738.     */
  739.     function writeDatasrc($datasrc, $configType, $options = array())
  740.     {
  741.         $configType = strtolower($configType);
  742.         if (!isset($GLOBALS['CONFIG_TYPES'][$configType])) {
  743.             return PEAR::raiseError("Configuration type '$configType' is not registered in Config_Container::writeDatasrc.", null, PEAR_ERROR_RETURN);
  744.         }
  745.         $includeFile = $GLOBALS['CONFIG_TYPES'][$configType][0];
  746.         $className = $GLOBALS['CONFIG_TYPES'][$configType][1];
  747.         include_once($includeFile);
  748.  
  749.         $writeMethodName = (version_compare(phpversion(), '5', '<')) ? 'writedatasrc' : 'writeDatasrc';
  750.         if (in_array($writeMethodName, get_class_methods($className))) {
  751.             $writer = new $className($options);
  752.             return $writer->writeDatasrc($datasrc, $this);
  753.         }
  754.  
  755.         // Default behaviour
  756.         $fp = @fopen($datasrc, 'w');
  757.         if ($fp) {
  758.             $string = $this->toString($configType, $options);
  759.             $len = strlen($string);
  760.             @flock($fp, LOCK_EX);
  761.             @fwrite($fp, $string, $len);
  762.             @flock($fp, LOCK_UN);
  763.             @fclose($fp);
  764.             return true;
  765.         } else {
  766.             return PEAR::raiseError('Cannot open datasource for writing.', 1, PEAR_ERROR_RETURN);
  767.         }
  768.     } // end func writeDatasrc
  769. } // end class Config_Container
  770. ?>
  771.